package de.poketradeshop.ui.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.inject.Singleton;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.google.common.util.concurrent.Service;

/**
 * A singleton class for sending mail messages.
 * @author tcolburn
 * Source not known
 */

public class MailService {

	private static MailService theService = null;
	private static Session mailSession;
	private static final String HOST = "smtp.gmail.com";
	private static final int PORT = 465;
	private static final String USER = "poketradeshop@gmail.com";
	private static final String PASSWORD = "!P0k3Tr4d35h0p#";
	private static final String FROM = "PokeTradeShop <poketradeshop@gmail.com>";
	private static List<String> mailContent = new ArrayList<String>();
	private static String pokemonList;
	private static String totalAmount;

	/**
     * Sends a subject and message to a recipient
     * @param recipient Internet address of the recipient
     * @throws MessagingException
     */
	
	public static void fillMail(String content){
		mailContent.add(content);
	}
	
	public static void clearMail(){
		mailContent.clear();
	}
	
	public static void fillMailValue(String content){
		totalAmount = content;
	}

	public static void sendMessageOrder(String recipient) throws MessagingException {

		if (theService == null) {
			theService = new MailService();
		}

		MimeMessage mimeMessage = new MimeMessage(mailSession);

		mimeMessage.setFrom(new InternetAddress(FROM));
		mimeMessage.setSender(new InternetAddress(FROM));
		mimeMessage.setSubject("Bestellung bei PokeTradeShop");
		
			
		for (int i = 0; i < mailContent.size(); i++) {
			if (i == 0){
				pokemonList = (mailContent.get(i) + "<br/><br/>");
			} else{
				pokemonList = (pokemonList + mailContent.get(i) + "<br/><br/>");
			}

		}
		
		mimeMessage.setContent("Vielen Dank für Ihre Bestellung bei PokeTradeShop. Wir melden uns in kürze bei Ihnen mit möglichen Tauschterminen! <br/><br/>" 
				+ pokemonList + totalAmount + "<br/><br/><br/>Mit freundlichem Gru&szlig <br/>Pok&eacute;TradeShop", "text/html");

		mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));

		Transport transport = mailSession.getTransport("smtps");
		transport.connect(HOST, PORT, USER, PASSWORD);

		transport.sendMessage(mimeMessage,
				mimeMessage.getRecipients(Message.RecipientType.TO));
		transport.close();

	}
	
	public static void sendMessageRegistration(String email) throws MessagingException {

		if (theService == null) {
			theService = new MailService();
		}

		MimeMessage mimeMessage = new MimeMessage(mailSession);

		mimeMessage.setFrom(new InternetAddress(FROM));
		mimeMessage.setSender(new InternetAddress(FROM));
		mimeMessage.setSubject("Registrierung bei PokeTradeShop");
		mimeMessage.setContent("Viele Dank für Ihre Registrierung bei PokeTradeShop. Wir wünschen Ihnen viel Spaß beim Einkauf. <br/> <br/> <br/> Pok&eacute;TradeShop", "text/html");

		mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(email));

		Transport transport = mailSession.getTransport("smtps");
		transport.connect(HOST, PORT, USER, PASSWORD);

		transport.sendMessage(mimeMessage,
				mimeMessage.getRecipients(Message.RecipientType.TO));
		transport.close();

	}

	public static void sendMessageUpdate(String email) throws MessagingException {

		if (theService == null) {
			theService = new MailService();
		}

		MimeMessage mimeMessage = new MimeMessage(mailSession);

		mimeMessage.setFrom(new InternetAddress(FROM));
		mimeMessage.setSender(new InternetAddress(FROM));
		mimeMessage.setSubject("Profiländerung bei PokeTradeShop");
		mimeMessage.setContent("Ihre Profildaten wurden auf PokeTradeShop wurden geändert. Sollte dies nicht durch Sie veranlasst worden sein, wenden Sie sich bitte an die Adminitration. <br/> <br/> <br/> Pok&eacute;TradeShop", "text/html");

		mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(email));

		Transport transport = mailSession.getTransport("smtps");
		transport.connect(HOST, PORT, USER, PASSWORD);

		transport.sendMessage(mimeMessage,
				mimeMessage.getRecipients(Message.RecipientType.TO));
		transport.close();

	}
	
	private MailService() {
		Properties props = new Properties();

		props.put("mail.transport.protocol", "smtps");
		props.put("mail.smtps.host", HOST);
		props.put("mail.smtps.auth", "true");
		props.put("mail.smtp.from", FROM);
		props.put("mail.smtps.quitwait", "false");

		mailSession = Session.getDefaultInstance(props);
		mailSession.setDebug(true);
	}

}